import com.alibaba.ttl.TransmittableThreadLocal;
import com.alibaba.ttl.threadpool.TtlExecutors;

import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author shxl2021
 * @Date 2023/7/28 16:55
 * @Version 1.0
 */
public class ThreadLoaclHelper implements Runnable {
    private static ThreadLocal<String> tl = new TransmittableThreadLocal<>();

    public static void main(String[] args) {


        tl.set("main thread");
        ExecutorService ttlExecutorService = TtlExecutors.getTtlExecutorService(Executors.newFixedThreadPool(2));
        for (int i = 0; i < 3; i++) {
            ttlExecutorService.submit(new ThreadLoaclHelper());
        }
        System.out.println("main end:::::" + tl.get());

        while (true) {
            Scanner scanner = new Scanner(System.in);
            String s = scanner.nextLine();
            tl.set(s);
        }


    }

    @Override
    public void run() {
        String o = tl.get();
        Thread currentThread = Thread.currentThread();
        System.out.println(currentThread.getName() + currentThread.getName() + "\t" + o);
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        tl.remove();

    }
}
